# -*- coding: utf-8 -*- 
import datetime
import logging
import urllib2

from google.appengine.ext import db
from BeautifulSoup import BeautifulSoup

from model import *

def update_site_sold_total():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos
    
    # update groups
    for group_info in today_groups:
        update_group_sold_total(group_info)

def update_group_sold_total(group_info):
    page = urllib2.urlopen(group_info.link)
    soup = BeautifulSoup(page)

    update_group(group_info, soup)

    group_info.put()

    logging.info("groupinfo updated succeed : %s"%group_info.link)

def get_site():
    group_site = GroupSite.get_by_key_name("SITE_nuomi")
    if group_site is None:
        group_site = GroupSite(key_name="SITE_nuomi")
        group_site.code = "nuomi"
        group_site.name = "糯米".decode('utf8')
        group_site.link = db.Link("http://www.nuomi.com/beijing")
        group_site.pop = 90
        group_site.put()
    
    return group_site

def update_group(group_info,soup):
    link = soup.findAll('meta',attrs={'property' : 'og:url'})
    link = link[0]['content']
    
    picture = soup.findAll('meta',attrs={'property' : 'og:image'})
    picture = picture[0]['content']
    
    title = soup.findAll('meta',attrs={'property' : 'og:description'})
    title = title[0]['content']
    title = title.replace("\"", "")
    
    current_price = soup.findAll('p',attrs={'class' : 'cur-price'})
    current_price = current_price[0].contents[1]
    current_price = current_price.replace(',','')
    
    table = soup.findAll('div',attrs={'class' : 'deal-discount'})
    tr = table[0].findAll("tr")[1]
    td = tr.findAll("td")
    
    original_price = td[0].findAll("strong")[0].contents[0][1:]
    original_price = original_price.replace(',','')
    discount = td[1].findAll("strong")[0].contents[0][:-1]
    
    sold_total = soup.findAll('div', attrs={'class':'deal-state'})[0].findAll('span', attrs={'class':'num'})[0].getText()

    group_info.title = title
    group_info.current_price = float(current_price)
    group_info.original_price = float(original_price)
    group_info.discount = float(discount)
    group_info.picture = db.Link(picture)
    group_info.link = db.Link(link)
    group_info.sold_total = int(sold_total)

def get_data():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos

    if today_groups.count() == 0:
        
        page = urllib2.urlopen(group_site.link)
        soup = BeautifulSoup(page)
        
        items = soup.findAll('div', attrs={'class':'sub-deal box2'})
        for item in items:    
            title_h = item.findAll('h3')[0]
            title = title_h.getText()
            link = title_h.findAll('a')[0]['href']

            picture = item.findAll('div',attrs={'class':'deal-pic'})[0].findAll('img')[0]['src']

            deal_div = item.findAll('div',attrs={'class':'deal-info'})[0]

            current_price = deal_div.findAll('div',attrs={'class':'entrance'})[0].findAll('span',attrs={'class' : 'price'})[0].contents[2]

            trs = deal_div.findAll('div',attrs={'class' : 'discount'})[0].findAll('tr')   
            original_price = trs[0].findAll("strike")[0].contents[1][1:]
            discount = trs[2].findAll("strong")[0].contents[0][:-1]

            sold_total = deal_div.findAll('span', attrs={'class':'num'})[0].contents[0]

            #create
            group_info = GroupInfo()
            group_info.site = group_site
            group_info.title = title
            group_info.current_price = float(current_price)
            group_info.original_price = float(original_price)
            group_info.discount = float(discount)
            group_info.picture = db.Link(picture)
            group_info.link = db.Link(link)
            group_info.sold_total = int(sold_total)
            group_info.create()
            
            group_infos.append(group_info)

    else:
        for group in today_groups:
            group_infos.append(group)
        
    return group_infos


